package com.feng.thread;

import java.util.Random;

/**
 * 需求： 大气环境数据为：温度、湿度和风速 一个大气环境传感装置 测量环境数据需要5s时间 一个计算机读取传感器的环境数据需要0.01s，
 * 模拟一个计算机读取大气环境传感器读取的大气环境数据100次
 * 
 * @author Qian
 *
 */
public class AtmosphereTest {

	public static void main(String[] args) {
		// 实体类，封装温度、湿度和风速三个参数
		Atmosphere atmosphere = new Atmosphere();
		// 传感器读取大气数据
		AtmosphereSensor atmosphereSensor = new AtmosphereSensor(atmosphere);
		// 计算机读取传感器数据
		Computer computer = new Computer(atmosphereSensor);
		// 线程
		// new Thread(atmosphereSensor).start();
		new Thread(computer).start();
	}

}

/**
 * 传感器 1.5秒/次 读取大气信息
 * 
 * @author 795829
 * 
 */
class AtmosphereSensor implements Runnable {
	private Atmosphere atmosphere;

	public AtmosphereSensor(Atmosphere atmosphere) {
		this.atmosphere = atmosphere;
	}

	public Atmosphere getAtmosphere() {
		return atmosphere;
	}

	public void setAtmosphere(Atmosphere atmosphere) {
		this.atmosphere = atmosphere;
	}

	@Override
	public void run() {
		try {
			for (int i = 1; i <= 10; i++) {
				Thread.sleep(5000);
				atmosphere.setHumidity(new Random(System.currentTimeMillis()).nextFloat());
				atmosphere.setTemperature(new Random(System.currentTimeMillis()).nextFloat());
				atmosphere.setWind(new Random(System.currentTimeMillis()).nextFloat());
				System.out.println("----- " + i + " 传感器数据    温度 = " + atmosphere.getTemperature() + ", 湿度 = "
						+ atmosphere.getHumidity() + ", 风速 = " + atmosphere.getWind());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

/**
 * 计算机 0.01秒/次 读取传感器数据
 * 
 * @author 795829
 * 
 */
class Computer implements Runnable {

	private AtmosphereSensor atmosphereSensor;

	public Computer(AtmosphereSensor atmosphereSensor) {
		this.atmosphereSensor = atmosphereSensor;
	}

	public AtmosphereSensor getAtmosphereSensor() {
		return atmosphereSensor;
	}

	public void setAtmosphereSensor(AtmosphereSensor atmosphereSensor) {
		this.atmosphereSensor = atmosphereSensor;
	}

	@Override
	public void run() {
		new Thread(atmosphereSensor).start();// 执行大气传感器读取大气数据的线程
		try {
			for (int i = 1; i <= 10; i++) {
				Thread.sleep(5010);// 等待时间为传感器读取时间+计算机读取传感器的时间
				System.out.println(i + " 计算机数据    温度 = " + atmosphereSensor.getAtmosphere().getTemperature() + ", 湿度 = "
						+ atmosphereSensor.getAtmosphere().getHumidity() + ", 风速 = "
						+ atmosphereSensor.getAtmosphere().getWind());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

class Atmosphere {
	private float temperature;
	private float humidity;
	private float wind;

	public float getTemperature() {
		return temperature;
	}

	public void setTemperature(float temperature) {
		this.temperature = temperature;
	}

	public float getHumidity() {
		return humidity;
	}

	public void setHumidity(float humidity) {
		this.humidity = humidity;
	}

	public float getWind() {
		return wind;
	}

	public void setWind(float wind) {
		this.wind = wind;
	}
}
